<?php

declare(strict_types=1);

namespace app\user\controller\admin;

use think\Request;
use sheep\controller\Backend;
use sheep\exception\SheepException;
use think\facade\Db;
use app\user\service\Wallet as WalletService;
use app\user\model\User as UserModel;

class User extends Backend
{
    protected $model = null;

    protected function initialize()
    {
        $this->model = new UserModel;
    }

    /**
     * 用户列表
     *
     * @return \think\Response
     */
    public function list()
    {
        $data = $this->model->sheepFilter()->paginate((int)request()->param('list_rows', 10));

        return success('获取成功', $data);
    }

    /**
     * 用户详情
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail($id)
    {
        $user = $this->model->with(['third_oauth', 'parent_user'])->findOrFail($id);

        return success('获取成功', $user);
    }

    /**
     * 更新用户(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $params = $this->request->only(['username', 'nickname', 'mobile', 'password', 'avatar', 'gender', 'email', 'status']);

        if (empty($params['password'])) unset($params['password']);
        if (empty($params['username'])) unset($params['username']);

        $id = explode(',', $id);
        foreach ($id as $i) {
            $params['id'] = $i;
            $this->svalidate($params, '\app\user\validate\User.edit');
            unset($params['id']);
        }

        Db::transaction(function () use ($id, $params) {
            foreach ($this->model->whereIn('id', $id)->cursor() as $user) {
                $user->save($params);
            }
        });

        return success('更新成功');
    }

    /**
     * 删除用户(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $is_real = $this->request->param('is_real');
        // TODO: 前端is_real判断不正确
        $is_real = 0;

        $id = explode(',', $id);

        Db::transaction(function () use ($id, $is_real) {
            foreach ($this->model->whereIn('id', $id)->cursor() as $user) {
                // 真实删除
                $is_real && $user->force();
                $user->delete();
            }
        });

        return success('删除成功');
    }

    public function recharge()
    {
        $params = $this->request->only(['id', 'type', 'amount', 'memo']);
        if (!in_array($params['type'], ['money', 'score'])) {
            throw new SheepException('参数错误');
        }

        $result = Db::transaction(function () use ($params) {
            return WalletService::change($params['id'], $params['type'], $params['amount'], 'admin_recharge', [], $params['memo']);
        });
        if ($result) {
            return success('充值成功');
        }
        return error('充值失败');
    }



    public function select()
    {
        $data = $this->model->sheepFilter()->paginate((int)request()->param('list_rows', 10));

        return success('获取成功', $data);
    }
}
